#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: sdr1k_controller.py
Date: August 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:
The hardware interface to the SDR1000

"""

#-----------------------------------------------------------------------
# System imports
import logging
import sys
import os
import Pyro.core
import Pyro.naming

# Application imports
import sdr1khw
import config_service as config

#======================================================================
# The SDR1000 controller class
# This is just a wrapper onto sdr1khw.py which is the SWIG generated file
# that binds to the 'C' shared library where the actual controller code lives.
class Sdr1kController:
    
    #-------------------------------------------------------------------
    def __init__(self, es):
        
        # Stash the event service
        self.es = es
        
        # Initialise the frequency
        self.freq = 0.0
        
        # Initialise the controller. Note these values are stored in options.conf
        self.hw = None
        self.hw = sdr1khw.SDR1000('pylink', config.sdr1000_rfe(), config.sdr1000_pa(), config.sdr1000_usb(), config.sdr1000_addr())
        
    #-------------------------------------------------------------------
    # Message handlers
    # Note some also reflect the change. Try not to double up on these messages.
    #-------------------------------------------------------------------
    def start_hw(self, data):
        self.hw.PowerOn()
        
    #-------------------------------------------------------------------
    def stop_hw(self, data):
        self.hw.StandBy()
        
    #-------------------------------------------------------------------
    def set_freq(self, data):
        self.hw.SetFreq(data)
        self.freq = data
        self.es.publish('ui_hints', ('freq_change', self.freq))
        
    #-------------------------------------------------------------------
    def freq_inc(self, data):
        new_freq = self.freq + data
        if new_freq > 0 and new_freq < 60.0:
            self.freq = self.freq + data
            self.hw.SetFreq(self.freq)
            self.es.publish('ui_hints', ('freq_change', self.freq))
    
    #-------------------------------------------------------------------
    def init_freq(self, data):
        self.hw.SetFreq(data)
        self.freq = data